<?php

class Playlist
{
    private $id;
    private $name;
    private $xml;
    private $userid;

    public function __construct($id, $name, $xml, $userid) {
        $this->id = $id;
        $this->name = $name;
        $this->xml = $xml;
        $this->userid = $userid;
        
    }

    public function get_id() {
        return $this->id;
    }
    
    public function set_id($id) {
        $this->id = $id;
    }
    
    public function get_name() {
        return $this->name;
    }

    public function get_xml() {
        return $this->xml;
    }

    public function get_userid() {
        return $this->userid;
    }
    
    public function createXML() {
        $dom = new DOMDocument();    
        $songlist = $dom->appendChild($dom->createElement('songlist')); 
        $songlist->appendChild($dom->createTextNode($this->id));
        $dom->formatOutput = true;
        $dom->save('./Playlists/' . 'U' . $this->userid . 'P' . $this->id .  '.xml'); 
        $this->xml = './Playlists/' . 'U' . $this->userid . 'P' . $this->id .  '.xml';
    }

    public function add_song($id3) {
        $dom = new DOMDocument();
        $dom->load('./Playlists/' . 'U' . $this->userid . 'P' . $this->id .  '.xml');
              
        foreach($dom->getElementsByTagName('songlist') as $songlist) { // Solo habrá una
            $song = $songlist->appendChild($dom->createElement('song'));
            
                $id = $song->appendChild($dom->createElement('id'));
                $id->appendChild($dom->createTextNode($id3->get_id()));
                $image = $song->appendChild($dom->createElement('image'));
                $image->appendChild($dom->createTextNode($id3->get_image()));
                $artist = $song->appendChild($dom->createElement('artist'));
                $artist->appendChild($dom->createTextNode($id3->get_artist()));
                $title = $song->appendChild($dom->createElement('title'));
                $title->appendChild($dom->createTextNode($id3->get_title()));
                $track = $song->appendChild($dom->createElement('track'));
                $track->appendChild($dom->createTextNode($id3->get_track()));
                $year = $song->appendChild($dom->createElement('year'));
                $year->appendChild($dom->createTextNode($id3->get_year()));
                $album = $song->appendChild($dom->createElement('album'));
                $album->appendChild($dom->createTextNode($id3->get_album()));
                $album = $song->appendChild($dom->createElement('public'));
                $album->appendChild($dom->createTextNode($id3->get_album()));
        }
        $dom->save('./Playlists/' . 'U' . $this->userid . 'P' . $this->id .  '.xml'); 
    }
    
    public function delete_song($id_song) {
        $dom = new DOMDocument();
        $dom->load('./Playlists/' . 'U' . $this->userid . 'P' . $this->id .  '.xml');

	foreach ($dom->getElementsByTagName('song') as $song) {
	    foreach(($song->childNodes) as $s) {
		if (is_a($s, 'DOMElement')) {
		     if ($s->tagName == 'id' && $s->textContent == $id_song) {
                         $s->parentNode->removeChild($s);
                         /*$parent = $s->parentNode;
                         $parent->removeChild($parent);*/
			 //$songlist = $dom->getElementsByTagName('songlist')->item(0);
			 //$songlist->removeChild($song);
			 break;
		     }
		}
	    }
	}
        $dom->save('./Playlists/' . 'U' . $this->userid . 'P' . $this->id .  '.xml');

	/*foreach($dom->getElementsByTagName('songlist') as $songlist) { // Solo habrá una
	    foreach($songlist->getElementsByTagName("song") as $song) {
		$id = $song->getElementsByTagName("id")->item(0);
		if ($id->textContent == $id_song) {
		    var_dump($song);
		    $songlist->removeChild($song);
		    break;
		}
	    }
	}*/
	
    }  
    
    public function get_playlist() {
        $dom = new DOMDocument();
        $dom->load('./Playlists/' . 'U' . $this->userid . 'P' . $this->id .  '.xml');
	
	foreach ($dom->getElementsByTagName('song') as $song) {
	    foreach(($song->childNodes) as $s) {
		if (is_a($s, 'DOMElement')) {
		     if ($s->tagName == 'id') $id = htmlentities($s->textContent);                    
		     if ($s->tagName == 'image') $image = htmlentities($s->textContent);
		     if ($s->tagName == 'artist') $artist = htmlentities($s->textContent);
		     if ($s->tagName == 'title') $title = htmlentities($s->textContent);
		     if ($s->tagName == 'track') $track = htmlentities($s->textContent);
		     if ($s->tagName == 'year') $year = htmlentities($s->textContent);
		     if ($s->tagName == 'album') $album = htmlentities($s->textContent);
		     if ($s->tagName == 'public') $public = htmlentities($s->textContent);
		}
	    }
            $playlist[] = array('id' => $id, 'image' => $image, 'artist' => $artist, 'title' => $title,
		'track' => $track, 'year' => $year, 'album' => $album, 'public' => $public);
	}
        return isset($playlist) ? $playlist : false;
    }
    
}

?>
